<?php
namespace ServerLoveMCPE;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\CommandExecutor;
use pocketmine\event\Listener;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Config;
use pocketmine\event\player\PlayerJoinEvent;

class Main extends PluginBase implements Listener{

    /** @var $nolove Config */
    private $nolove;
    /** @var $config Config */
    private $config;

    public function onEnable(){
        $this->getServer()->getPluginManager()->registerEvents($this, $this);
        
		// Make plugin data folder
		if(!is_dir($this->getDataFolder())){
			@mkdir($this->getDataFolder());
		}
		
		$this->getLogger()->info("§b加载配置中……");
		
        $this->nolove = new Config($this->getDataFolder()."nolove.yml", Config::YAML);
        $this->config = new Config($this->getDataFolder()."love.yml", Config::YAML);
		
		$this->getLogger()->info("§b配置已加载！");
    }
    
	public function onJoin(PlayerJoinEvent $event){
		$thisplayer = $event->getPlayer();
		
		// how many people love you	
		$loves = $this->config->get('players.'.strtolower($thisplayer->getName()), []);
		if($loves === null) $loves = [];
			
		$lovenum = count($loves);
		$thisplayer->setDisplayName(str_replace(TextFormat::LIGHT_PURPLE . "[<3]", "", $thisplayer->getDisplayName()));
		
		if($lovenum > 0){
			$thisplayer->setDisplayName(TextFormat::LIGHT_PURPLE . "[<3]" . TextFormat::WHITE . $thisplayer->getDisplayName());
		}
	}
	
    public function onCommand(CommandSender $sender, Command $command, string  $label, array $args) : bool {
        switch($command->getName()){
/**
*
*                                         LOVE Command
*
**/
            case "love":
                if(!(isset($args[0]))){
                    return false;
                }
                if (!($sender instanceof Player)){ 
                $sender->sendMessage("§5[<3] 对不起，你必须在游戏中使用这个命令。");
                    return true;
                }
                
                $loved = array_shift($args);
				$lovedPlayer = $this->getServer()->getPlayer($loved);
				
				if($lovedPlayer !== null and $lovedPlayer->isOnline()){
					$loved = $lovedPlayer->getName();
                
					if($this->nolove->exists(strtolower($loved))){
						$sender->sendMessage("§5[<3]对不起， " . $loved . "§5 现在不想去爱任何人。");
						return true;
					}else{
                        if($lovedPlayer === $sender){
                            $sender->sendMessage("§5[<3]你不能爱自己!");
                            
                        }else{
							
                            $loves = $this->config->get('players.'.strtolower($lovedPlayer->getName()), []);
                            if($loves === null) $loves = [];
							
							$lovenum = count($loves);
							
							if(in_array(strtolower($sender->getName()), $loves, false)) {
								$sender->sendMessage(TextFormat::RED.'你已经爱上了那个玩家。');
								return true;
							}
							
                            if(count($loves) >= 20) {
                                $sender->sendMessage(TextFormat::RED.'你不能爱超过20个玩家。');
                                return true;
                            }

                            $loves[] = strtolower($sender->getName());

                            $this->config->set('players.'.strtolower($lovedPlayer->getName()), $loves);
                            $this->config->save();

                            $lovedPlayer->sendMessage("§5[<3]§a" . $sender->getName() . "§5爱上你了!");
                            if(isset($args[0])){
                                $lovedPlayer->sendMessage("Reason: " . implode(" ", $args));
                            }
                            $sender->sendMessage("§5[<3] 所以你爱 §a" . $loved . "？§5 哇,那不错");
                            $this->getServer()->broadcastMessage("§a" . $sender->getName() . " §dis in love with §a" . $loved . "§d.");
                            $this->getServer()->broadcastMessage("§d♥" . $loved . "§d ♥ " . $sender->getName() . "§d♥");
                            
							// If it's his first love
							if($lovenum === 0){
								$lovedPlayer->setDisplayName(TextFormat::LIGHT_PURPLE . "[<3]" . TextFormat::WHITE . $lovedPlayer->getDisplayName());
							}
                            
                            return true;
                        }
					}
                }else{
                    $sender->sendMessage("§5[<3] §a" . $loved . "§5 无法被爱 §a" . $loved . "§5 不存在，或不在线。");
                    return true;
                }
/**
*
*                                 BREAKUP Command
*
**/
                break;
            case "breakup":
                if(!(isset($args[0]))){
                    return false;
                }
                if (!($sender instanceof Player)){ 
                $sender->sendMessage("§5[<3] 对不起，你必须在游戏中使用这个命令。");
                    return true;
                }
                $loved = array_shift($args);
                    $lovedPlayer = $this->getServer()->getPlayer($loved);
                    if($lovedPlayer !== null and $lovedPlayer->isOnline()){
                        $loved = $lovedPlayer->getName();

                        $loves = $this->config->get('players.'.strtolower($lovedPlayer->getName()), []);

                        if($loves === null) $loves = [];

                        if(!in_array(strtolower($sender->getName()), $loves, false)) {
                            $sender->sendMessage(TextFormat::RED.'你不喜欢那个玩家。');
                            return true;
                        }

                        foreach($loves as $key => $value) {
                            if($value === strtolower($sender->getName())) {
                                unset($loves[$key]);
                            }
                        }

                        if(count($loves) === 0) {
							/*NAMETAG THING */
							//$sender->setDisplayName(str_replace(TextFormat::LIGHT_PURPLE . "[<3]", "", $sender->getDisplayName()));
							$lovedPlayer->setDisplayName(str_replace(TextFormat::LIGHT_PURPLE . "[<3]", "", $lovedPlayer->getDisplayName()));
							/*NAMETAG THING */
							
                            $this->config->remove('players.' . strtolower($lovedPlayer->getName()));
                        } else {
                            $this->config->set('players.'.strtolower($lovedPlayer->getName()), $loves);
                        }

                        $this->config->save();
                        
                        $lovedPlayer->sendMessage("§5[<3]§a" . $sender->getName() ."§5已经和你分手了!");
                        if(isset($args[0])){
                            $lovedPlayer->sendMessage("原因: " . implode(" ", $args));
                        }
                        $sender->sendMessage("§5[<3] 你已经和 §a" . $loved . "§5分手了。");
                        $this->getServer()->broadcastMessage("§d[<3]§a" . $sender->getName() . " §d和 §a" . $loved . "§d分手了。");
                        
                        return true;
                    }else{
                        $sender->sendMessage($loved . "§5[<3] 不可分手. 主要是因为, §a" . $loved . "§5 不存在，或不在线。");
                        return true;
                    }
/**
*
*                                      NOLOVE Command
*
**/
            break;
            case "nolove":
                if(!(isset($args[0]))){
                    return false;
                }
                if (!($sender instanceof Player)){ 
                $sender->sendMessage("§5[<3] 对不起，你必须在游戏中使用这个命令。");
                    return true;
                }
                if($args[0] == "nolove"){
                    $this->nolove->set(strtolower($sender->getName()));
                    $sender->sendMessage("§5[<3] 你将不再被爱。 §e#注孤生啦……");
					$this->nolove->save();
                    return true;
                }elseif($args[0] == "love"){
                    $this->nolove->remove(strtolower($sender->getName()));
                    $sender->sendMessage("§5[<3] 你已经可以被爱了! §e#大胆去爱吧！");
					$this->nolove->save();
                    return true;
                }else{
                    return false;
                }
/**
*
*                                   SERVERLOVEMCPE Commands 
*
**/
            break;
            case "serverlove":
                $sender->sendMessage("§5[<3][ServerLoveMCPE] ServerLoveMCPE插件指令: ");
                $sender->sendMessage("§d[<3][ServerLoveMCPE] 用法: /爱 <玩家名称>");
                $sender->sendMessage("§d[<3][ServerLoveMCPE] 用法: /分手 <玩家名称>");
                $sender->sendMessage("§d[<3][ServerLoveMCPE] 用法: /状态 <nolove / love> ");
                $sender->sendMessage("§5[<3][ServerLoveMCPE] 祝你幸福~！~！");
                return true;
            break;
        default:
            return false;
        }
    return false;
    }
}
